#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.12.0
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

#Include <Array.au3>
#Include <Constants.au3>
#Include <File.au3>
#Include <Process.au3>
#Include <String.au3>

$sSource = "Y:\"
$sFinal  = "Y:\"

If StringRight( $sSource , 1 ) <> '\' Then $sSource &= '\'
If StringRight( $sFinal  , 1 ) <> '\' Then $sFinal  &= '\'

$cExtensions = '*.3gp;*.avi;*.avs;*.divx;*.f4v;*.flac;*.flv;*.m2ts;*.m4v;*.mkv;*.mp3;*.mp4;*.mpg;*.ogg;*.ogm;*.ts;*.vob'

$aAllFiles = _FileListToArrayRec( $sSource , $cExtensions , 1 , 1 , 0 , 2 )
If IsArray( $aAllFiles ) Then
	For $ii = 1 To $aAllFiles[0]
		If StringInStr( $aAllFiles[$ii] , '#recycle' ) Then ContinueLoop
		_Macros( $aAllFiles[$ii] )

		; Build Array of Streams 1
		$sRandomString = _RandomString()
		RunWait( @ComSpec & ' /c ffprobe.exe -show_streams "' & $aAllFiles[$ii] & '" >"' & @TempDir & '\' & $sRandomString & '.txt"' , StringLeft( @ScriptDir , StringInStr( @ScriptDir , '\' , 0 , -1 )) & 'BIN\FFMpeg\' , @SW_HIDE )
		; Build Array of Streams 2
		$aAllStreams = _StringBetween( FileRead( @TempDir & '\' & $sRandomString & '.txt' ) , '[STREAM]' , '[/STREAM]' )
		RunWait( @ComSpec & ' /c FileASSASSIN.exe /assassinate /silent "' & @TempDir & '\' & $sRandomString & '.txt"' , StringLeft( @ScriptDir , StringInStr( @ScriptDir , '\' , 0 , -1 )) & 'BIN\FileASSASSIN\' , @SW_HIDE )

		; Create Stream Count for All Streams
		$iAllStreamsCount = UBound( $aAllStreams )

		; Find Video Streams
		$aFindVideoStreams = _ArrayFindAll( $aAllStreams , 'codec_type=video' , 0 , 0 , 0 , 1 )
		If $aFindVideoStreams <> -1 Then
			; Process Video Streams
			For $aa = 0 To UBound( $aFindVideoStreams ) - 1
				$iSVideoIndex	    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?index=(\S+))?'                )
				$sSVideoCodec	    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?codec_name=(\S+))?'           )
				$sSVideoFourCC	    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?codec_tag_string=(\S+))?'     )
				$iSVideoWidth	    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?width=(\S+))?'                )
				$iSVideoHeight	    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?height=(\S+))?'               )
				$fSVideoSAR		    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?sample_aspect_ratio=(\S+))?'  )
				$fSVideoDAR		    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?display_aspect_ratio=(\S+))?' )
				$sSVideoPixel	    = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?pix_fmt=(\S+))?'              )
				$iSVideoNumerator  = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?r_frame_rate=(\d+)/)?'        )
				$iSVideoDenominator= _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?r_frame_rate=\d+/(\d+))?'     )
				$sSVideoLanguage   = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?language=(\S+))?'             )
				$sSVideoStartTime  = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?start_time=(\S+))?'           )
				$iSVideoDuration   = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?duration=(\S+))?'             )
				$sSVideoMetaLang   = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?TAG:language=(\S+))?'         )
				$sSVideoMetaTitle  = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?TAG:title=(\V+))?'            )
				$iSVideoFrames     = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?nb_frames=(\S+))?'            )

				$fSVideoSAR = _ColonToDecimal( $fSVideoSAR )
				$fSVideoDAR = _ColonToDecimal( $fSVideoDAR )

				FileSetAttrib( $sFileDir , '-RASHNOT' , 1 )
				FileSetAttrib( $sFileDir & '*' , '-RASHNOT' , 1 )
				DirCreate( $sFinal & '_' & $iSVideoWidth & '\' )
				DirMove( $sFileDir , $sFinal & '_' & $iSVideoWidth & '\' & $sParent , 1 )
			Next
		EndIf
	Next
EndIf

MsgBox ( 0 , 'Finished:' , 'Batch Operation Complete' )




Func _Macros( $sFile )
	Global $sFileDir    = StringLeft     ( $sFile     , StringInStr( $sFile     , '\' , 0 , -1 ) )
	Global $sFileExt    = StringTrimLeft ( $sFile     , StringInStr( $sFile     , '.' , 0 , -1 ) - 1 )
	Global $sFileName   = StringTrimLeft ( $sFile     , StringInStr( $sFile     , '\' , 0 , -1 ) )
	Global $sShortName  = StringLeft     ( $sFileName , StringInStr( $sFileName , '.' , 0 , -1 ) - 1 )
	Global $sParent     = StringTrimLeft ( $sFileDir  , StringInStr( $sFileDir  , '\' , 0 , -2 ) )
	Global $sFolderName = StringTrimRight( StringTrimLeft( $sFileDir , StringInStr( $sFileDir , '\' , 0 , -2 ) ) , 1 )
EndFunc

Func _ColonToDecimal( $sDecimal )
	If StringInStr( $sDecimal , ':' ) <> 0 Then
		$aValues = StringSplit( $sDecimal , ':' )
		Return ( Number( $aValues[1] , 3 ) / Number( $aValues[2] , 3 ))
	Else
		Return Number( $sDecimal , 3 )
	EndIf
EndFunc

Func _RandomString()
	$sString = ''
	Dim $aSpace[3]
	For $i = 1 To 15
		$aSpace[0] = Chr( Random( 65 ,  90 , 1 )) ; A-Z
		$aSpace[1] = Chr( Random( 97 , 122 , 1 )) ; a-z
		$aSpace[2] = Chr( Random( 48 ,  57 , 1 )) ; 0-9
		$sString &= $aSpace[Random( 0 , 2 , 1 )]
	Next
	Return $sString
EndFunc

Func _RegExString( $sString , $sRegEx )
	Local $sValue = _ArrayToString( StringRegExp( $sString , $sRegEx , 1 ))
	If StringIsFloat( $sValue ) = 1 OR StringIsInt( $sValue ) = 1 Then
		Return Number( $sValue )
	Else
		If $sValue = 'N/A' OR $sValue = 'N' OR $sValue = 'A' Then
			Return ''
		Else
			Return $sValue
		EndIf
	EndIf
EndFunc
